package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.StudentMapper;
import com.example.pojo.Student;
import com.example.pojo.dto.pagedto.StudentPageDTO;
import com.example.pojo.vo.StudentVO;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author wk
 * @since 2025-04-02
 */
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private  StudentMapper studentMapper;

    public StudentServiceImpl() {
        System.out.println("StudentServiceImpl 实例已创建");
    }

    @Override
    @Tool(name = "queryListByName", description = "根据学生姓名模糊查询学生信息")
    public List<StudentVO> queryListByName(@ToolParam(description = "学生姓名") String  name){
        //TODO 处理查询条件
        LambdaQueryWrapper<Student> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Student::getName,name);

        List<Student> list=studentMapper.selectList(wrapper);
        List<StudentVO> dataVOList = new ArrayList();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(p -> dataVOList.add(BeanUtil.toBean(p, StudentVO.class)));
        }
        return dataVOList;
    }



    @Override
    @Tool(name = "pageInfo", description = "根据条件分页查询学生信息")
    public IPage<StudentVO> pageInfo(@ToolParam(description = "学生分页信息") StudentPageDTO pageDTO){
        IPage<Student> page = new Page<>();
        page.setCurrent(pageDTO.getCurrent());
        page.setSize(pageDTO.getSize());
        //TODO 处理查询条件
        LambdaQueryWrapper<Student> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Student::getSex,pageDTO.getSex());
        wrapper.like(Student::getName,pageDTO.getName());
        wrapper.like(Student::getClassRoom,pageDTO.getClassRoom());
        wrapper.like(Student::getAddress,pageDTO.getAddress());
        wrapper.orderByDesc(Student::getId);

        IPage<Student> pageList = studentMapper.selectPage(page, wrapper);
        List<StudentVO> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(pageList.getRecords())) {
            pageList.getRecords().forEach(v -> {
                voList.add(BeanUtil.toBean(v, StudentVO.class));
            });
        }
        Page<StudentVO> result = new Page<>();
        BeanUtils.copyProperties(page,result);
        result.setRecords(voList);
        return result;
    }

}
